/**
 * @apiDefine ErrorResponse
 * @apiError (请求错误) {String} err_message 错误信息描述
 */

/**
 * @apiVersion 1.0.0
 * @apiGroup 运费
 * @api {GET} api/freightlist 运费列表
 * @apiName api/freightlist
 * @apiExample {js} axios 例子:
 *  axios.get('http://39.105.38.34:8080/api/freightlist', {
 *    params: {
 *      size: 10,
 *      page: 1,
 *    }
 *  });
 * @apiHeader {String} authorization 用户登录或注册成功时返回的 token
 * @apiParam {Number} [page=1] 分页的页数
 * @apiParam {Number} [size=10] 每页的条数
 * @apiSuccess (请求成功) {Array} freightlist 运费列表
 * @apiuse  ErrorResponse
 */


const { query } = require('../../helper/db');
const { door, ok } = require('../../helper/util');


module.exports = async ctx => {
  var token = ctx.headers.authorization;
  var user = await door(token);
  var params = ctx.query;
  var sql = ['select * from freight where 1 = 1'];
  var values = [];
  var size = params.size ? params.size : 10;
  var page = params.page ? params.page : 1;
  var end = size * page;

  sql.push('order by id desc limit ?, ?');

  values.push(end - 10);
  values.push(end);

  var freightlist = await query(sql.join(' '), values);

  ctx.body = ok({ freightlist });
}
